package com.cwf.org.service.impl;

import com.cwf.basic.service.impl.BaseServiceImpl;
import com.cwf.org.domain.Employee;
import com.cwf.org.dto.EmployeeRoleDTO;
import com.cwf.org.mapper.EmployeeMapper;
import com.cwf.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void saveRoles(EmployeeRoleDTO employeeRoleDTO) {
        employeeMapper.deleteRolesById(employeeRoleDTO.getId());
        if (!employeeRoleDTO.getRoles().isEmpty()){
            employeeMapper.saveRoles(employeeRoleDTO);
        }
    }

    @Override
    public List<Long> getRolesById(Long id) {
        return employeeMapper.getRolesById(id);
    }

    @Override
    public List<Employee> queryByKeyWords(String keyword) {
        return employeeMapper.queryByKeyWords(keyword);
    }

    @Override
    public void delete(Serializable id) {
        // 1.清空用户绑定的角色关系
        employeeMapper.truncateRolesByUserId(id);
        // 2.删除角色
        employeeMapper.delete(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        // 批量删除同样需要清空用户与角色的关系
        employeeMapper.truncateRolesByIds(ids);
        employeeMapper.batchDelete(ids);
    }
}
